//
//  DisherDetailViewController.m
//  Recipe
//
//  Created by bing han on 12-7-11.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DisherDetailViewController.h"
#import "OrderList.h"
#import "RequireViewController.h"

@interface DisherDetailViewController ()

@end

@implementation DisherDetailViewController
@synthesize pricecount = _pricecount, count = _count;
@synthesize dishername = _dishername, disherdetail = _disherdetail, disherpicture = _disherpicture;
@synthesize substractBtn = _substractBtn;
@synthesize diserkey = _diserkey;
@synthesize backBtn = _backBtn;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void) viewWillAppear:(BOOL)animated{
    [_substractBtn setHidden:YES];
}

-(void) initWithDict:(NSDictionary*) disher count:(NSInteger) cnt{
    _diserkey = [disher objectForKey:@"businesskey"];
    _dishername.text = [disher objectForKey:@"cnamecn"];
    _disherdetail.text = [disher objectForKey:@"remarks"];
    
    NSString *imagepath = [disher objectForKey:@"bigimage"];
    UIImage *bigimage = [UIImage imageWithContentsOfFile:imagepath];
    
    _disherpicture.image = bigimage;
    _pricecount.text = [disher objectForKey:@"cprice"];
    _count.text = [NSString stringWithFormat:@"%d",cnt];
    if (cnt > 0) {
        [_count setHidden:NO];
        [_substractBtn setHidden:NO];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(IBAction)addDisher:(id)sender{
    [_substractBtn setHidden:NO];
    
    NSInteger tmpcount = [_count.text intValue];
    tmpcount += 1;
    [self updateOrderList:1];
    _count.text = [NSString stringWithFormat:@"%d",tmpcount];
}

-(IBAction)subtractDisher:(id)sender{
    NSInteger tmpcount = [_count.text intValue];
    if (tmpcount >=1) {
        tmpcount -= 1;
        [self updateOrderList:-1];
        if (tmpcount == 0) {
            [_substractBtn setHidden:YES];
        }
        _count.text = [NSString stringWithFormat:@"%d",tmpcount];
    }
}

//更新系统点菜列表
-(void) updateOrderList:(NSInteger) value{
    OrderList *ol = [OrderList newInstance];
    if (value > 0) {
        [ol addDisher:_diserkey];
    }else{
        [ol substractDisher:_diserkey];
    }    
}

-(IBAction)moreReuest:(id)sender{
//    CGRect rvcRect = CGRectMake(0.0f, 0.0f, 423.0f, 297.0f);
    
    RequireViewController *rvc = [[RequireViewController alloc] initWithNibName:@"RequireViewController" bundle:nil];
//    [rvc.view setFrame:rvcRect];
//    rvc.view.backgroundColor =[ UIColor clearColor];
//    rvc.view.frame.size = rvcSize;
    rvc.disherkey = _diserkey;
    rvc.delegate = self;
    rvc.modalInPopover = NO;
    rvc.modalPresentationStyle = UIModalPresentationFormSheet;
    rvc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    
    [self presentModalViewController:rvc animated:YES];
    
    [rvc release];
}

-(void) dissmissRequireView{
     [[self modalViewController] dismissModalViewControllerAnimated:YES];
}

-(IBAction)orderDisher:(id)sender{
    [_substractBtn setHidden:NO];
    
    NSInteger tmpcount = [_count.text intValue];
    tmpcount += 1;
    [self updateOrderList:1];
    _count.text = [NSString stringWithFormat:@"%d",tmpcount];
}

-(IBAction)goBack:(id)sender{
    _backBtn.enabled = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    _backBtn = nil;
    _substractBtn = nil;
    _pricecount = nil;
    _count = nil;
    _dishername = nil;
    _disherdetail = nil;
    _disherpicture = nil;
    _diserkey = nil;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}


@end

